using System;
using System.Xml;
using System.Collections.Generic;

namespace ScriptingEngineModule
{
    class GameModuleBuilder
    {   
        private const string GAME_TAG = @"GAME";
        private const string ROUNDMANAGER_TAG = @"ROUNDMANAGER";
        private const string ROUND_TAG = @"ROUND";
        private const string MOVE_TAG = @"MOVE";
        private const string TURN_TAG = @"TURN";
        
        private string definitionFile;
        private string gameModulePath;
        
        public GameModuleBuilder(string gameModulePath, string definitionFile)
        {
            this.gameModulePath = gameModulePath;
            this.definitionFile = definitionFile;
        }
        
        public GameModule build()
        {
            XmlDocument xmlDoc= new XmlDocument(); 
            xmlDoc.Load(gameModulePath + definitionFile);

            XmlNodeList manager = xmlDoc.GetElementsByTagName("roundmanager");
            XmlNodeList rounds = xmlDoc.GetElementsByTagName("round");
            XmlNodeList turns = xmlDoc.GetElementsByTagName("turn");
            XmlNodeList moves = xmlDoc.GetElementsByTagName("move");
            
            //gdzie sprawdzanie czy dane nie sa uszkodzone??
            GameModule module = new GameModule(this.gameModulePath + manager[0].InnerText);            
            node2List(rounds, module.Rounds);
            node2List(moves, module.Moves);
            node2List(turns, module.Turns);
            
            return module;
        }
        
        private void node2List(XmlNodeList node, List<string> list)
        {
            if(null != node && null != list) 
            {
                for (int i = 0; i < node.Count; i++)
                {
                    list.Add(this.gameModulePath + node[i].InnerText);
                }
            }
            //TODO: exception??
        }
    }
}